<template>
  <div class="login_container">
    <div class="login_box">
      <div class="top_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录验证 -->
      <el-form ref="formRef" :model="loginForm" class="demo-dynamic">
        <el-form-item
          prop="username"
          :rules="[
            {
              required: true,
              message: '请输入用户名',
              trigger: 'blur',
            },
          ]"
        >
          <el-input :prefix-icon="Avatar" v-model="loginForm.username" />
        </el-form-item>
        <el-form-item
          prop="password"
          :rules="[
            {
              required: true,
              message: '请输入密码',
              trigger: 'blur',
            },
          ]"
        >
          <el-input
            :prefix-icon="Lock"
            v-model="loginForm.password"
            type="password"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="login(formRef)">登录</el-button>
          <el-button @click="res">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
import { Avatar, Lock } from "@element-plus/icons-vue";
import service from "../until/service";

const formRef = ref<FormInstance>();
const router = useRouter();
const loginForm = reactive<{
  username: string;
  password: string;
}>({
  username: "admin",
  password: "123456",
});

// 登录验证
const login = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate((valid) => {
    if (valid) {
      console.log(valid);
      service
        .post("login", loginForm)
        .then(({ data }) => {
          if (data.meta.status == 200) {
            // 提示信息
            ElMessage({
              message: data.meta.msg,
              type: "success",
            });
            window.sessionStorage.setItem("token", data.data.token);
            router.push("/");
          } else {
            ElMessage({
              message: data.meta.msg,
              type: "error",
            });
          }
        })
        .catch((err) => console.log(err));
    } else {
      console.log("error submit!");
      return false;
    }
  });
};

// 重置
const res = () => {
  loginForm.username = "admin";
  loginForm.password = "123456";
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.top_box {
  width: 130px;
  height: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 150px;
  top: -65px;
  background-color: #ddd;
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
}
.demo-dynamic {
  padding: 20px;
  margin-top: 80px;
}
.demo-dynamic button {
}
</style>
